package com.genesis.web.controller.usermember;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.genesis.mall.common.Result;
import com.genesis.mall.dto.SmsFlashPromotionSessionDto;
import com.genesis.mall.entity.*;
import com.genesis.mall.service.*;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 前台首页内容
 */

@RestController
@RequestMapping("/home")
public class HomeController {


    @Autowired
    private ISmsHomeAdvertiseService smsHomeAdvertiseService;

    @Autowired
    private IUmsMemberService umsMemberService;

    @Autowired
    private ISmsHomeBrandService iSmsHomeBrandService;

    @Autowired
    private IPmsBrandService pmsBrandService;


    @Autowired
    private ISmsHomeRecommendProductService recommendProductService;

    @Autowired
    private ISmsHomeNewProductService newProductService;

    @Autowired
    private IPmsProductService productService;
    //秒杀
    @Autowired
    private ISmsFlashPromotionSessionService iSmsFlashPromotionSessionService;

    @Autowired
    private ISmsFlashPromotionProductRelationService iSmsFlashPromotionProductRelationService;

    @Autowired
    private IPmsProductService iPmsProductService;

    @Autowired
    private ICmsSubjectService iCmsSubjectService;

    /**
     * 首页滚动
     *
     * @param request
     * @return
     */

    @GetMapping("/content")
    public Result context(HttpServletRequest request) {

//          String token = request.getHeader("Authorization");
//          String token1 = token.substring(6);
//          Claims claims = TokenUtil.getClaims(token1);
//          Long umsMemberId = Long.parseLong(claims.get("umsMember").toString()) ;
//          UmsMember umsMember = umsMemberService.getById(umsMemberId);


        //1. 轮播广告
        List<SmsHomeAdvertise> advertiseList = smsHomeAdvertiseService.list();

        //2.   推荐品牌
        List<SmsHomeBrand> brandLists = iSmsHomeBrandService.list();
        ArrayList<PmsBrand> brandList = new ArrayList<>();
        for (SmsHomeBrand smsHomeBrand : brandLists) {
            Long brandId = smsHomeBrand.getBrandId();
            LambdaQueryWrapper<PmsBrand> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsBrand::getId, brandId);
            PmsBrand brand = pmsBrandService.getOne(queryWrapper);
            brandList.add(brand);
        }

        //2.1 秒杀

        SmsFlashPromotionSessionDto smsFlashPromotionSessionDto = new SmsFlashPromotionSessionDto();


        List<SmsFlashPromotionSession> list1 = iSmsFlashPromotionSessionService.list();
        for (SmsFlashPromotionSession smsFlashPromotionSession : list1) {
            BeanUtils.copyProperties(smsFlashPromotionSession, smsFlashPromotionSessionDto);
            LambdaQueryWrapper<SmsFlashPromotionProductRelation> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(SmsFlashPromotionProductRelation::getFlashPromotionSessionId, smsFlashPromotionSession.getId());
            List<SmsFlashPromotionProductRelation> list2 = iSmsFlashPromotionProductRelationService.list(queryWrapper);//* 商品限时购与商品关系表

            ArrayList<PmsProduct> pmsProductList = new ArrayList<>();            //子类集合
            for (SmsFlashPromotionProductRelation smsFlashPromotionProductRelation : list2) {  //遍历 商品限时购与商品关系表
                LambdaQueryWrapper<PmsProduct> queryWrapper1 = new LambdaQueryWrapper<>();
                queryWrapper1.eq(PmsProduct::getId, smsFlashPromotionProductRelation.getProductId());
                PmsProduct pmsProduct = iPmsProductService.getOne(queryWrapper1);
                pmsProductList.add(pmsProduct);

            }

            smsFlashPromotionSessionDto.setProductList(pmsProductList);
        }

        //3.最新商品列表 newProductList
        List<SmsHomeNewProduct> newProductLists = newProductService.list();
        ArrayList<PmsProduct> newProductList = new ArrayList<>();
        for (SmsHomeNewProduct smsHomeNewProduct : newProductLists) {
            Long productId = smsHomeNewProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(lqw);
            newProductList.add(pmsProduct);
        }

        //5.最热商品 hotProductList
        List<SmsHomeRecommendProduct> list = recommendProductService.list();
        ArrayList<PmsProduct> hotProductList = new ArrayList<>();
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
            Long productId = smsHomeRecommendProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(queryWrapper);
            hotProductList.add(pmsProduct);
        }


        //6.推荐专题 subjectList

        List<CmsSubject> subjectList = iCmsSubjectService.list();

        HashMap<Object, Object> map = new HashMap<>();
        map.put("advertiseList", advertiseList);
        map.put("brandList", brandList);
        map.put("homeFlashPromotion", smsFlashPromotionSessionDto);
        map.put("newProductList", newProductList);
        map.put("hotProductList", hotProductList);
        map.put("subjectList", subjectList);

        return Result.success(map, "操作成功");
    }


    /**
     * 最新產品
     *
     * @param request
     * @return
     */
    @GetMapping("/newProductList")
    public Result<List<PmsProduct>> newProductList(HttpServletRequest request) {

        List<SmsHomeNewProduct> newProductLists = newProductService.list();
        ArrayList<PmsProduct> newProductList = new ArrayList<>();
        for (SmsHomeNewProduct smsHomeNewProduct : newProductLists) {
            Long productId = smsHomeNewProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> lqw = new LambdaQueryWrapper<>();
            lqw.eq(PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(lqw);
            newProductList.add(pmsProduct);
        }
        return Result.success(newProductList, "操作成功");
    }

    /**
     * 最熱商品
     *
     * @param request
     * @return
     */
    @GetMapping("/hotProductList")
    public Result<List<PmsProduct>> hotProductList(HttpServletRequest request) {
        List<SmsHomeRecommendProduct> list = recommendProductService.list();
        ArrayList<PmsProduct> hotProductList = new ArrayList<>();
        for (SmsHomeRecommendProduct smsHomeRecommendProduct : list) {
            Long productId = smsHomeRecommendProduct.getProductId();
            LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(PmsProduct::getId, productId);
            PmsProduct pmsProduct = productService.getOne(queryWrapper);
            hotProductList.add(pmsProduct);
        }
        return Result.success(hotProductList, "操作成功");
    }

    /**
     *
     获取首页商品分类
     * @param parentId
     * @param request
     * @return
     */
    @GetMapping("/productCateList/{parentId}")
    public Result productCateList(@PathVariable Long parentId, HttpServletRequest request) {

        List<PmsProductCategory> productCateList = iPmsProductService.groupBy(parentId);

        return Result.success(productCateList);
    }

    /**
     * 分页获取推荐商品
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/recommendProductList")
    public Result recommendProductList(@RequestParam int pageNum, @RequestParam int pageSize){
        //1.分页构造器
        Page<PmsProduct > page = new Page<>(pageNum, pageSize);
        //2.条件构造器
        LambdaQueryWrapper<PmsProduct> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByDesc(PmsProduct::getSort);
        Page<PmsProduct> pmsProductPage = iPmsProductService.page(page,queryWrapper);

        List<PmsProduct> records = pmsProductPage.getRecords();

        ArrayList<PmsProduct> list = new ArrayList<>();
        for (PmsProduct record : records) {
            PmsProduct pmsProduct = new PmsProduct();
            BeanUtils.copyProperties(record,pmsProduct);
            list.add(pmsProduct);
        }
        return  Result.success(list);
    }

    /**
     * 根据分类获取专题
     * @param cateId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/subjectList")
    public  Result subjectList(@RequestParam  Long cateId,int pageNum,int pageSize){

        //1.分页构造器
        Page<CmsSubject> CmsSubjectPage = new Page<>(pageNum, pageSize);

        //2.条件构造器
        LambdaQueryWrapper<CmsSubject> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotEmpty(cateId.toString()),CmsSubject::getCategoryId,cateId);

        //3.
        Page<CmsSubject> cmsSubjectPage = iCmsSubjectService.page(CmsSubjectPage, queryWrapper);

        ArrayList<CmsSubject> subjectList = new ArrayList<>();
        List<CmsSubject> records = cmsSubjectPage.getRecords();

        for (CmsSubject record : records) {

            subjectList.add(record);

        }
        return  Result.success(subjectList);

    }

}
